package org.light.simpleauth.verb;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Dropdown;
import org.light.domain.Field;
import org.light.domain.FieldSerialComparator;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Type;
import org.light.domain.Var;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.oracle.generator.MybatisOracleSqlReflector;
import org.light.utils.DomainUtil;
import org.light.utils.FieldUtil;
import org.light.utils.MybatisSqlReflector;
import org.light.utils.PgsqlReflector;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class FindIndexedName extends Verb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("findIndexedName");
			method.addSignature(new Signature(1, "db","*sql.DB"));
			method.addSignature(new Signature(2, "name", "string"));
			method.setReturnType(this.domain.getDomainName().getRawType());
			method.addAdditionalImport("strings");
			
			List<Writeable> sList = new ArrayList<Writeable>();
			
			sList.add(new Statement(1000L,1,"shortName := name"));
			sList.add(new Statement(2000L,1,"if strings.Contains(name, \"_\") && utils.IsDigit(name[strings.LastIndex(name, \"_\")+1:len(name)]) {"));
			sList.add(new Statement(3000L,2,"shortName = name[0:strings.LastIndex(name, \"_\")]"));
			sList.add(new Statement(4000L,1,"}"));
			sList.add(new Statement(5000L,0,""));
			sList.add(new Statement(7000L,0,""));
			
			if (StringUtil.isBlank(this.getDbType())||this.getDbType().equalsIgnoreCase("MariaDB")||this.getDbType().equalsIgnoreCase("MySQL")) {
				sList.add(new Statement(8000L,1,"sql := \"select "+StringUtil.changeDomainFieldtoTableColum(this.domain.findFieldByFixedName("userName").getLowerFirstFieldName())+" from "+DomainUtil.getTableName(this.domain)+" where "+StringUtil.changeDomainFieldtoTableColum(this.domain.findFieldByFixedName("userName").getLowerFirstFieldName())+" like concat(? ,'%') \""));
			}else if (this.getDbType().equalsIgnoreCase("PostgreSQL")||this.getDbType().equalsIgnoreCase("pgsql")) {
				sList.add(new Statement(8000L,1,"sql := \"select "+StringUtil.changeDomainFieldtoTableColum(this.domain.findFieldByFixedName("userName").getLowerFirstFieldName())+" from "+DomainUtil.getTableName(this.domain)+" where "+StringUtil.changeDomainFieldtoTableColum(this.domain.findFieldByFixedName("userName").getLowerFirstFieldName())+" like $1 || '%' \""));
			}else if (this.getDbType().equalsIgnoreCase("Oracle")) {
				sList.add(new Statement(8000L,1,"sql := \"select "+StringUtil.changeDomainFieldtoTableColum(this.domain.findFieldByFixedName("userName").getLowerFirstFieldName())+" from "+DomainUtil.getTableName(this.domain)+" where "+StringUtil.changeDomainFieldtoTableColum(this.domain.findFieldByFixedName("userName").getLowerFirstFieldName())+" like concat( :"+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+",'%') \""));
			}	
			
			sList.add(new Statement(9000L,1,"stmt, err := db.Prepare(sql)"));
			sList.add(new Statement(10000L,1,"defer stmt.Close()"));
			sList.add(new Statement(11000L,1,"if err != nil {"));
			sList.add(new Statement(12000L,2,"panic(err)"));
			sList.add(new Statement(13000L,1,"}"));
			sList.add(new Statement(14000L,1,"rows, err := stmt.Query(shortName)"));
			sList.add(new Statement(15000L,1,"if err != nil {"));
			sList.add(new Statement(16000L,2,"panic(err)"));
			sList.add(new Statement(17000L,1,"}"));
			sList.add(new Statement(18000L,1,"var names []string"));
			sList.add(new Statement(19000L,0,""));
			sList.add(new Statement(20000L,1,"for rows.Next() {"));
			sList.add(new Statement(21000L,2,"var "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+" string"));
			sList.add(new Statement(22000L,2,"err := rows.Scan(&"+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+")"));
			sList.add(new Statement(23000L,2,"if err != nil {"));
			sList.add(new Statement(24000L,3,"panic(err)"));
			sList.add(new Statement(25000L,2,"}"));
			sList.add(new Statement(26000L,2,"names = append(names, "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+")"));
			sList.add(new Statement(27000L,1,"}"));
			sList.add(new Statement(28000L,0,""));
			sList.add(new Statement(29000L,1,"indexNum := 1"));
			sList.add(new Statement(30000L,1,"for pos := range names {"));
			sList.add(new Statement(32000L,2,"if strings.Contains(names[pos], \"_\") && utils.IsDigit(names[pos][strings.LastIndex(names[pos], \"_\")+1:len(names[pos])]) {"));
			sList.add(new Statement(33000L,3,"index, _ := strconv.Atoi(names[pos][strings.LastIndex(names[pos], \"_\")+1:len(names[pos])])"));
			sList.add(new Statement(34000L,3,"if index >= indexNum {"));
			sList.add(new Statement(35000L,4,"indexNum = index + 1"));
			sList.add(new Statement(36000L,3,"}"));
			sList.add(new Statement(37000L,2,"}"));
			sList.add(new Statement(38000L,1,"}"));
			sList.add(new Statement(39000L,0,""));
			sList.add(new Statement(40000L,1,"indexName := shortName + \"_\" + strconv.Itoa(indexNum)"));
			sList.add(new Statement(41000L,1,"return indexName"));
			
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;		
	}

	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}
	
	public FindIndexedName(){
		super();
		this.setLabel("找出索引名字");
	}
	
	public FindIndexedName(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("Clone");
		this.setVerbName("FindIndexedName");
		this.setLabel("找出索引名字");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("FindIndexedName");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		return null;
	}
	
	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;		
	}
	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;		
	}
}
